package cn.tedu.medicalmanagementsystem.manager.service.impl;

import cn.tedu.medicalmanagementsystem.base.exception.ServiceException;
import cn.tedu.medicalmanagementsystem.base.response.StatusCode;
import cn.tedu.medicalmanagementsystem.manager.mapper.ServiceTypeMapper;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.ServiceTypeAddParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.ServiceTypeDelParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.ServiceTypeListQuery;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.ServiceTypeUpdateParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.entity.ServiceType;
import cn.tedu.medicalmanagementsystem.manager.pojo.vo.ServiceTypeVO;
import cn.tedu.medicalmanagementsystem.manager.service.ServiceTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ServiceTypeServiceImpl implements ServiceTypeService {

    @Autowired
    ServiceTypeMapper serviceTypeMapper;

    //    添加服务类型
    @Override
    public ServiceTypeVO add(ServiceTypeAddParam serviceTypeAddParam) {
        if (serviceTypeAddParam == null) {
            throw new ServiceException(StatusCode.DATA_UNEXISTS); // 参数为空
        }
        if (serviceTypeAddParam.getName() == null || serviceTypeAddParam.getName().trim().isEmpty()) {
            throw new ServiceException(StatusCode.DATA_UNEXISTS);
        }

        ServiceType serviceType = new ServiceType();
        BeanUtils.copyProperties(serviceTypeAddParam,serviceType);
        serviceTypeMapper.add(serviceType);
        return serviceTypeMapper.selectById(serviceType.getId());
    }



    //    更新服务类型
    @Override
    public ServiceTypeVO update(ServiceTypeUpdateParam serviceTypeUpdateParam) {
        log.debug("开始处理更新服务类型业务，参数: {}", serviceTypeUpdateParam);

        ServiceTypeVO existingVO = serviceTypeMapper.selectById(serviceTypeUpdateParam.getId());
        if (existingVO == null) {
            throw new ServiceException(StatusCode.DATA_UNEXISTS);
        }

        // 如果 name 被修改，则检查是否重复
        if (!existingVO.getName().equals(serviceTypeUpdateParam.getName())) {
            ServiceTypeVO duplicate = serviceTypeMapper.selectByName(serviceTypeUpdateParam.getName());
            if (duplicate != null) {
                throw new ServiceException(StatusCode.USERNAME_EXISTS);
            }
        }

        ServiceType serviceType = new ServiceType();
        BeanUtils.copyProperties(serviceTypeUpdateParam, serviceType);
        serviceTypeMapper.update(serviceType);

        return serviceTypeMapper.selectById(serviceType.getId());
    }

    //    删除服务类型
    @Override
    public void delete(ServiceTypeDelParam serviceTypeDelParam) {
        log.debug("开始处理删除服务类型业务，参数: {}", serviceTypeDelParam);

        serviceTypeMapper.deleteById(serviceTypeDelParam.getId());
    }

    //    查询所有服务类
    @Override
    public List<ServiceTypeVO> allTypes() {
        return serviceTypeMapper.allTypes();
    }

    //  根据id查找服务类型
    @Override
    public ServiceTypeVO selectById(Long id) {
        System.out.println("查询的id是======================="+id);
        return serviceTypeMapper.selectById(id);
    }

    //    根据服务名称查找服务类型
    @Override
    public ServiceTypeVO selectByName(String name) {
        return serviceTypeMapper.selectByName(name);

    }

    @Override
    public List<ServiceTypeVO> selectServiceType(ServiceTypeListQuery serviceTypeListQuery) {
        log.debug("开始处理查询服务类型列表业务，参数: {}", serviceTypeListQuery);
        List<ServiceTypeVO> list = serviceTypeMapper.selectServiceType(serviceTypeListQuery);
        return list;
    }



}
